package com.atguigu.gmall.pms.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuEntity;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuMapper;
import com.atguigu.gmall.pms.vo.SaleAttrValueVo;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.service.SkuAttrValueService;
import org.springframework.util.CollectionUtils;


@Service("skuAttrValueService")
public class SkuAttrValueServiceImpl extends ServiceImpl<SkuAttrValueMapper, SkuAttrValueEntity> implements SkuAttrValueService {
    @Autowired
    private AttrMapper attrMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;


    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuAttrValueEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuAttrValueEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<SkuAttrValueEntity> querySkuAttrValueByCidAndSkuId(Long cid, Long skuId) {
        //根据cid查询检索类型的规格参数
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", cid)
                .eq("search_type", 1);
        List<AttrEntity> attrEntities = attrMapper.selectList(wrapper);

        //获取规格参数的ID的集合
        if (CollectionUtils.isEmpty(attrEntities)) {
            return null;
        }
        List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());

        //结合skuId 和 attrIds查询销售类型的规格参数和值
        QueryWrapper<SkuAttrValueEntity> queryWrapper = new QueryWrapper<>();
        //attrid 在集合内,就说明是对应的销售属性
        queryWrapper.eq("sku_id", skuId)
                .in("attr_id", attrIds);
        List<SkuAttrValueEntity> attrValueEntities = this.list(queryWrapper);
        return attrValueEntities;
    }

    @Override
    public List<SaleAttrValueVo> querySaleAttrValuesBySpuId(Long spuId) {
        //根据spuId查询spu下的sku
        List<SkuEntity> skuEntities = skuMapper.selectList(new QueryWrapper<SkuEntity>().eq("spu_id", spuId));
        if (CollectionUtils.isEmpty(skuEntities)){
            return null;
        }

        //获取所有的skuids
        List<Long> skuIds = skuEntities.stream().map(SkuEntity::getId).collect(Collectors.toList());

        //根据skuIds查询spu下所有销售属性
        List<SkuAttrValueEntity> skuAttrValueEntities = this.list(new QueryWrapper<SkuAttrValueEntity>().in("sku_id", skuIds));
        if (CollectionUtils.isEmpty(skuAttrValueEntities)){
            return null;
        }

        //数据转化
        //通过AttrId 分组,分成三组  Map<attrId, List<SkuAttrValueEntity>>
        Map<Long, List<SkuAttrValueEntity>> map = skuAttrValueEntities.stream().collect(Collectors.groupingBy(SkuAttrValueEntity::getAttrId));

        //把map转化成如下格式
        List<SaleAttrValueVo> saleAttrValueVos = new ArrayList<>();

        // [{attrId: 3, attrName: 机身颜色, attrValues: ["暗夜黑", "白天白"]},
        // {attrId: 4, attrName: 运行内存, attrValues: ["8G", "12G"]},
        // {attrId: 5, attrName: 机身存储, attrValues: ["128G", "256G"]}]
        map.forEach((attrId,skuAttrValueEntityList)->{
            SaleAttrValueVo saleAttrValueVo = new SaleAttrValueVo();
            //给vo设置参数
            saleAttrValueVo.setAttrId(attrId);
            saleAttrValueVo.setAttrName(skuAttrValueEntityList.get(0).getAttrName());
            //获取规格参数值的列表,设置给saleAttrValueVos
            Set<String> collect = skuAttrValueEntityList.stream().map(SkuAttrValueEntity::getAttrValue).collect(Collectors.toSet());
            saleAttrValueVo.setAttrValues(collect);

            saleAttrValueVos.add(saleAttrValueVo);
        });

        return saleAttrValueVos;
    }

    @Override
    public String queryMappingBySpuId(Long spuId) {
        //根据spuId查询spu下的sku
        List<SkuEntity> skuEntities = skuMapper.selectList(new QueryWrapper<SkuEntity>().eq("spu_id", spuId));
        if (CollectionUtils.isEmpty(skuEntities)){
            return null;
        }

        //获取所有的skuids
        List<Long> skuIds = skuEntities.stream().map(SkuEntity::getId).collect(Collectors.toList());

        //查询skuIds和销售属性组合的映射关系  {"暗夜黑,8G,128G": 100, "暗夜黑,12G,256G": 101}
        List<Map<String, Object>> maps = skuAttrValueMapper.queryMappingBySkuIds(skuIds);
        Map<String, Long> mapping = maps.stream().collect(Collectors.toMap(map -> map.get("attr_values").toString(), map -> (Long) map.get("sku_id")));
        return JSON.toJSONString(mapping);
    }

}